package com.camera.yl.v.models

import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.camera.base.BaseApp
import com.camera.base.inerface.ICPInterface
import com.camera.base.util.YLBaseConfig
import com.camera.base.util.YLCameraConfig
import com.camera.base.util.YLIMConfig
import com.camera.base.v.models.BaseViewModel
import com.camera.base.v.models.SystemNavBarViewModel
import com.camera.yl.R
import com.camera.yl.v.data.NumberPickerShow
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class MediaSettingViewModel @Inject constructor(
        icpInterface: ICPInterface?,
        val systemNavBarViewModel: SystemNavBarViewModel
) : BaseViewModel() {
        val TAG = MediaSettingViewModel::class.java.simpleName
        lateinit var mIcpInterface:ICPInterface
        val recordSize: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }
        val subSize: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }
        val subfps: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }
        val rateRecord: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }
        val rateSub: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }

        val recordSegmented: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }
        val windowManagerVisibility: MutableLiveData<Int> by lazy {
                MutableLiveData<Int>()
        }
        val cyclicStorageVisibility: MutableLiveData<Int> by lazy {
                MutableLiveData<Int>(View.VISIBLE)
        }
        val externalStorageVisibility: MutableLiveData<Int> by lazy {
                MutableLiveData<Int>(View.GONE)
        }
        val cyclicStorageChoose: MutableLiveData<Boolean> by lazy {
                MutableLiveData<Boolean>(true)
        }
        val externalStorageChoose: MutableLiveData<Boolean> by lazy {
                MutableLiveData<Boolean>(true)
        }
        val capture: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }
        val cameraStreamRotate: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }
        val preDelayRecordVisibility: MutableLiveData<Int> by lazy {
                MutableLiveData<Int>()
        }
        val preRecordVisibility: MutableLiveData<Int> by lazy {
                MutableLiveData<Int>()
        }
        val preRecordSwitch: MutableLiveData<Boolean> by lazy {
                MutableLiveData<Boolean>()
        }
        val preRecordTime: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }
        val delayRecordVisibility: MutableLiveData<Int> by lazy {
                MutableLiveData<Int>()
        }
        val delayRecordSwitch: MutableLiveData<Boolean> by lazy {
                MutableLiveData<Boolean>()
        }
        val delayRecordTime: MutableLiveData<String> by lazy {
                MutableLiveData<String>()
        }
        val videoDecoderChoose: MutableLiveData<Boolean> by lazy {
                MutableLiveData<Boolean>(true)
        }
        val videoEncoderGone: MutableLiveData<Boolean> by lazy {
                MutableLiveData<Boolean>(true)
        }

        val cameraApiChoose: MutableLiveData<Boolean> by lazy {
                MutableLiveData<Boolean>()
        }

        private val _showNumberPickerDialog = MutableLiveData<NumberPickerShow>()
        val showNumberPickerDialog: LiveData<NumberPickerShow> = _showNumberPickerDialog
        var SS:String? = null
        var MM:String? = null
        init {
                icpInterface?.let {
                        mIcpInterface = it
                        recordSize.value = it.recordSize.width.toString() + "x" + it.recordSize.height.toString()
                        subSize.value = it.subSize.width.toString() + "x" + it.subSize.height.toString()
                        subfps.value = it.subFPS.toString()
                        rateRecord.value = it.recordRate.let { "${String.format("%.1f", it.toDouble() / (1024.0 * 1024))}M" }.toString()
                        rateSub.value = it.subStreamRate.toString()
                        capture.value = it.captureChoose.let { if (it == 0) { "高" }else if (it == 1){ "中" } else{ "低" } }
                        cameraStreamRotate.value = it.getStreamRotate(it.currentCameraID).toString()
                        if(it.preRecordTimeChoose == -1){
                            preRecordVisibility.value = View.GONE
                        }else{
                            preRecordSwitch.value = it.preRecordTimeSwitch
                            preRecordTime.value = it.preRecordTime.toString()+"秒"
                            preRecordVisibility.value = View.VISIBLE
                        }
                        if (it.delayRecordTimeChoose == -1){
                            delayRecordVisibility.value = View.GONE
                        }else{
                            delayRecordSwitch.value = it.delayRecordTimeSwitch
                            delayRecordTime.value = it.delayRecordTime.toString()
                            delayRecordVisibility.value = View.VISIBLE
                        }
                        videoEncoderGone.value = BaseApp.ylBean.allConfig.encodeType.visibility
                        preDelayRecordVisibility.value = if (it.preRecordTimeChoose == -1 && it.delayRecordTimeChoose == -1) View.GONE else View.VISIBLE
                        windowManagerVisibility.value = if (BaseApp.ylBean.allConfig.userWindowManager.visibility) View.VISIBLE else View.GONE
                        cyclicStorageVisibility.value = if (BaseApp.ylBean.allConfig.cyclic.visibility) View.VISIBLE else View.GONE
                        externalStorageVisibility.value = if (BaseApp.ylBean.allConfig.useSDCardMode > 2) View.VISIBLE else View.GONE
                        windowManagerChoose.value = YLIMConfig.windowManagerChoose
                        cyclicStorageChoose.value = YLBaseConfig.cyclicStorage
                        externalStorageChoose.value = YLBaseConfig.externalStorage
                        videoDecoderChoose.value = it.videoDecoderType
                        cameraApiChoose.value = it.getCameraApi()
                }
        }

        override fun onCleared() {
                super.onCleared()
                YLCameraConfig.setSubStreamRate(rateSub.value!!.toInt())
        }
        fun setWindowManagerChoose(){
                YLIMConfig.setWindowManagerChoose(windowManagerChoose.value as Boolean)
        }
        fun setDelayRecordSwitch(){
                mIcpInterface.delayRecordTimeSwitch = delayRecordSwitch.value as Boolean
        }
        fun setVideoDecoderType(){
                mIcpInterface.videoDecoderType = videoDecoderChoose.value as Boolean
        }
        
        fun setCameraApiChoose(){
                val useCamera2 = cameraApiChoose.value as Boolean
                mIcpInterface.setCameraApi(useCamera2)
        }
        
        fun setPreRecordSwitch(){
                mIcpInterface.preRecordTimeSwitch = preRecordSwitch.value as Boolean
                if (preRecordSwitch.value as Boolean) {
                        //开启预录
                        if (!mIcpInterface.isPreRecord){
                                mIcpInterface.startPreRecord()
                        }
                }else{
                        //关闭预录
                        mIcpInterface.stopPreRecord()
                }
        }
        fun showRecordSegmented(mm :String,ss :String){
                recordSegmented.value = mIcpInterface.recordTime.let {
                        val minutes = if(it >= 60000) it / 60000 else it/1000 // 1000 * 60 = 60000
                        val minuteUnit = if(it >= 60000) mm else ss // 获取字符串资源
                        "${minutes} $minuteUnit"
                } ?: "N/A"
        }
        fun saveData(id :Int, postion :Int, data :String){
                when (id){
                        R.id.btn_video_record_segmented_set ->{
                                recordSegmented.value = data
                                mIcpInterface.setTimeChoose(postion)
                        }
                        R.id.btn_video_record_quality_set -> {
                                recordSize.value = data
                                mIcpInterface.setRecordChoose(postion)
                        }
                        R.id.btn_video_sub_strame_quality_set -> {
                                subSize.value = data
                                mIcpInterface.setSubStreamChoose(postion)
                        }
                        R.id.btn_video_sub_strame_fps_set -> {
                                subfps.value = data
                                mIcpInterface.subFPS = data.toInt()
                        }
                        R.id.btn_video_record_rate_set -> {
                                rateRecord.value = data
                                mIcpInterface.setRecordRateChoose(postion)
                        }
                        R.id.btn_capture_size_set -> {
                                capture.value = data
                                mIcpInterface.setCaptureChoose(postion)
                        }
                        R.id.btn_video_prerecord_set -> {
                                preRecordTime.value = data
                                mIcpInterface.preRecordTimeChoose = postion
                                //如果如果正在预录  实时改变预录时间  放到setPreRecordTimeChoose 方法里面了
                        }
                        R.id.btn_video_derecord_set -> {
                                delayRecordTime.value = data
                                mIcpInterface.delayRecordTimeChoose = postion
                                //如果如果正在延迟  实时改变预录时间
                        }
                        R.id.btn_video_sub_strame_rotate_set -> {
                                cameraStreamRotate.value = data
                                mIcpInterface.setStreamRotate(mIcpInterface.currentCameraID,data.toInt())
                        }
                }               
        }

        fun showNumberPickerDialog(id:Int){
                var options:Array<String>? = null
                when (id){
                    R.id.btn_video_record_segmented_set ->{
                        options = convertTimeToString(mIcpInterface.recordTimeArray)
                    }
                    R.id.btn_video_record_quality_set -> {
                        options = convertResolutionToString(mIcpInterface.recordSizeArray)
                    }
                   R.id.btn_video_sub_strame_quality_set -> {
                        options = convertResolutionToString(mIcpInterface.subStreamSizeArray)
                   }
                   R.id.btn_video_record_rate_set -> {
                        options = convertBytesToMegaBytes(mIcpInterface.recordRatesArray)
                   }
                   R.id.btn_video_sub_strame_fps_set -> {
                        options = (10..30).map { it.toString() }.toTypedArray()
                   }
                   R.id.btn_capture_size_set -> {
                        options = arrayOf("高","中","低")
                   }
                   R.id.btn_video_prerecord_set -> {
                        options = convertIntToString(mIcpInterface.preRecordTimeArray)
                   }
                   R.id.btn_video_derecord_set -> {
                        options = convertIntToString(mIcpInterface.delayRecordTimeArray)
                   }
                   R.id.btn_video_sub_strame_rotate_set -> {
                        options = arrayOf("0","90","180","270")
                   }
                }
                NumberPickerShow(id,true,options).also { _showNumberPickerDialog.value = it }
        }
        fun convertResolutionToString(resolutions: Array<IntArray>): Array<String> {
                return resolutions.map { "${it[0]}x${it[1]}" }.toTypedArray()
        }
        fun convertIntToString(resolutions: IntArray): Array<String> {
                return resolutions.map { "${it}" + "$SS" }.toTypedArray()
        }

        fun convertTimeToString(resolutions: IntArray): Array<String> {
                return resolutions.map { it.let {
                        val minutes = it / 60000 // 1000 * 60 = 60000
                        val minuteUnit = MM // 获取字符串资源
                        "${minutes} $minuteUnit"
                } ?: "N/A" }.toTypedArray()
        }

        fun convertBytesToMegaBytes(bytes: IntArray): Array<String> {
                return bytes.map { "${String.format("%.1f", it.toDouble() / (1024.0 * 1024))}M" }.toTypedArray()
        }
}